#include "playscene.h"
#include <QDebug>
#include <QString>
#include <QPainter>
#include <QPixmap>
#include <QMenuBar>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <mycoin.h>
#include "dataconfig.h"
#include <QMessageBox>

PlayScene::PlayScene(int LevelNum)
{
    QString str = QString("进入了第 %1 关").arg(LevelNum);
    qDebug()<< str;
    this->LevelIndex = LevelNum;

    //初始化场景
    this->setWindowTitle("游玩");
    this->setFixedSize(320,588);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    QMenu * startmenu = bar->addMenu("开始");
    QAction * quitaction = startmenu->addAction("退出");

    connect(quitaction,&QAction::triggered,[=](){
        this->close();
    });

    //back返回按钮
    MyPushButton * backbtn = new MyPushButton(":/res/BackButton.png");
    backbtn->setParent(this);
    backbtn->move(this->width()-backbtn->width(),this->height()-backbtn->height());

    connect(backbtn,&MyPushButton::clicked,[=](){
        qDebug() << "在playscene页面点击back按钮";
        QTimer::singleShot(300,this,[=](){
            emit PlaySceneBack();
        });
    });

    //左下角显示当前关卡数
    QLabel * label = new QLabel;
    label->setParent(this);

    //设置字体
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);

    //将字体设置到label控件中
    label->setFont(font);

    QString str1 = QString("Level: %1").arg(this->LevelIndex);
    label->setText(str1);

    //调整位置
    label->setGeometry(30,this->height()-50,120,50);


    //初始化 gameArray
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j] = config.mData[this->LevelIndex][i][j];
        }
    }


    //创建金币的背景图片
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QLabel * label = new QLabel;
            label->setGeometry(0,0,50,50);

            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->setParent(this);
            label->move(57+i*50,200+j*50);



            //创建金币
            QString str;
            if(this->gameArray[i][j] == 1)
            {
                //显示金币
                str = ":/res/Coin0001.png";
            }
            else
            {
                //显示银币
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);

            //给金币属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];  //1正面 0反面

            //将金币放入到金币的二维数组中，以便后期维护
            coinArray[i][j] = coin;

            //监听点击金币事件
            connect(coin,&MyCoin::clicked,[=](){
                //qDebug() << "点击金币";
                coin->ChangeFlag();
                //更新二维数组内容   如果是金币改成银币，银币改成金币
                this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;


                //翻转周围硬币,并延时300ms
                QTimer::singleShot(300,this,[=](){

                    //右侧硬币翻转条件
                    if(coin->posX < 3)
                    {
                        coinArray[coin->posX + 1][coin->posY]->ChangeFlag();
                        this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0;
                    }
                    //左侧硬币翻转条件
                    if(coin->posX > 0)
                    {
                        coinArray[coin->posX - 1][coin->posY]->ChangeFlag();
                        this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0;
                    }
                    //上侧硬币翻转条件
                    if(coin->posY > 0)
                    {
                        coinArray[coin->posX][coin->posY - 1]->ChangeFlag();
                        this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0;
                    }
                    //下侧硬币翻转条件
                    if(coin->posY < 3)
                    {
                        coinArray[coin->posX][coin->posY + 1]->ChangeFlag();
                        this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0;
                    }

                    //判断是否胜利
                    this->isWin = true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(this->coinArray[i][j]->flag == false)
                            {
                                this->isWin = false;
                                break;
                            }
                        }
                    }

                    if(this->isWin)
                    {
                        //游戏胜利
                        qDebug() << "Success";
                        QMessageBox * msgBox = new QMessageBox(QMessageBox::Information, "Success", "游戏胜利", QMessageBox::Ok);
                        msgBox->show();
                        //for(int i=0;i<4;i++)
                        //{
                            //for(int j=0;j<4;j++)
                            //{
                                //coinArray[i][j]->isWin = true;
                            //}
                        //}
                    }

                });

            });
        }
    }
}


//背景
void PlayScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width()) * 0.5,30,pix.width(),pix.height(),pix);
}
